//
//  XGMMViewController.m
//  FruitsWholesaler
//
//  Created by sgone-2 on 15/12/2.
//  Copyright © 2015年 sgone. All rights reserved.
//

#import "XGMMViewController.h"
#import "AFNetworking.h"
#import "Common.h"
#import "MBProgressHUD+MJ.h"

@interface XGMMViewController ()<UITextFieldDelegate>


@property(nonatomic,strong) UITextField *textMM1;
@property(nonatomic,strong) UITextField *textMM2;
@property(nonatomic,strong) UITextField *textMM3;

//网路请求
@property(nonatomic,strong)AFHTTPSessionManager *manager;

@end

@implementation XGMMViewController



//afnetworking网络请求数据
- (AFHTTPSessionManager *)manager
{
    if (_manager == nil) {
        _manager  = [AFHTTPSessionManager manager];
    }
    return _manager;
    
    
}



- (void)viewDidLoad {
    [super viewDidLoad];

    
    self.titleLabel.text = @"修改密码";
    self.backBtn.hidden = NO;
    self.rightBut.hidden = NO;
    self.rightBut.titleLabel.font = [UIFont systemFontOfSize:16];
    
    [self.rightBut setTitle:@"确定" forState:UIControlStateNormal];
    self.view.backgroundColor = BACKGROUND1;
    
    //直接添加修改密码
    
    [self initStyle];
    
}



-(void)initStyle
{

    UILabel *lbl1 = [[UILabel alloc]initWithFrame:CGRectMake(0, 64, ViewWidth, 44)];
    lbl1.text = @"  当前密码";
    [self.view addSubview:lbl1];
    lbl1.font = [UIFont systemFontOfSize:15];
    lbl1.textColor = [UIColor grayColor];
    
    
    UIView *view2 = [[UIView alloc]initWithFrame:CGRectMake(0, 64+44, 10, 50)];
    view2.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:view2];
    
    UIView *view23 = [[UIView alloc]initWithFrame:CGRectMake(ViewWidth-15, 64+44, 15, 50)];
    view23.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:view23];

    
    
    self.textMM1 = [[UITextField alloc]initWithFrame:CGRectMake(10, 64+44, ViewWidth-15, 50)];
    self.textMM1.placeholder = @"请输入当前密码";
    [self.view addSubview:self.textMM1];
    self.textMM1.backgroundColor = [UIColor whiteColor];
    self.textMM1.font = [UIFont systemFontOfSize:16];
    self.textMM1.secureTextEntry = YES;
    self.textMM1.clearButtonMode = UITextFieldViewModeWhileEditing;
    self.textMM1.delegate = self;
    self.textMM1.returnKeyType = UIReturnKeyDone;
    
    
    
    
    
    
    UILabel *lbl2 = [[UILabel alloc]initWithFrame:CGRectMake(0, 64+44 +50, ViewWidth, 44)];
    lbl2.text = @"  新密码";
    [self.view addSubview:lbl2];
    lbl2.font = [UIFont systemFontOfSize:15];
    lbl2.textColor = [UIColor grayColor];
    
    
    UIView *view3 = [[UIView alloc]initWithFrame:CGRectMake(0, 64+44*2+50, 10, 50)];
    view3.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:view3];
    
    UIView *view22 = [[UIView alloc]initWithFrame:CGRectMake(ViewWidth-15, 64+44*2+50, 15, 50)];
    view22.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:view22];

    
    
    
    self.textMM2 = [[UITextField alloc]initWithFrame:CGRectMake(10, 64+44*2+50, ViewWidth-15, 50)];
    self.textMM2.placeholder = @"请输入新密码";
    [self.view addSubview:self.textMM2];
    self.textMM2.backgroundColor = [UIColor whiteColor];
    self.textMM2.font = [UIFont systemFontOfSize:16];
    self.textMM2.secureTextEntry = YES;
    self.textMM2.clearButtonMode = UITextFieldViewModeWhileEditing;
    self.textMM2.delegate = self;
    self.textMM2.returnKeyType = UIReturnKeyDone;


    
    UIView *view4 = [[UIView alloc]initWithFrame:CGRectMake(0, 64+44*2+50+50, 10, 50)];
    view4.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:view4];
    
    UIView *view21 = [[UIView alloc]initWithFrame:CGRectMake(ViewWidth-15, 64+44*2+50+50, 15, 50)];
    view21.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:view21];

    
    
    
    self.textMM3 = [[UITextField alloc]initWithFrame:CGRectMake(10, 64+44*2+50+50, ViewWidth-15, 50)];
    self.textMM3.placeholder = @"请再次输入新密码";
    [self.view addSubview:self.textMM3];
    self.textMM3.backgroundColor = [UIColor whiteColor];
    self.textMM3.font = [UIFont systemFontOfSize:16];
    self.textMM3.secureTextEntry = YES;
    self.textMM3.clearButtonMode = UITextFieldViewModeWhileEditing;
    self.textMM3.delegate = self;
    self.textMM3.returnKeyType = UIReturnKeyDone;

    
    
    
    //中间的线
    UIView *view10 = [[UIView alloc]initWithFrame:CGRectMake(10, 64+44*2+50+50, ViewWidth-10, 0.8)];
    view10.backgroundColor = BACKGROUND1;
    [self.view addSubview:view10];

    




}


#pragma mark 确认修改密码
- (void)rightButClick:(id)sender
{
    
    
    //放弃第一响应者身份
    
    
    [self.textMM1 resignFirstResponder];
    [self.textMM2 resignFirstResponder];
    [self.textMM3 resignFirstResponder];
    
     if (self.textMM1.text.length == 0)
    {
        
        [self.view makeToast:@"当前密码不能为空"];
        return;
        
    }
    
    else if (self.textMM2.text.length == 0)
    {
        
        [self.view makeToast:@"新密码不能为空"];
        return;
        
    }
    else if (self.textMM3.text.length == 0)
    {
        [self.view makeToast:@"请再次输入新密码"];
        return;
        
    }

   else if (![self.textMM1.text isEqualToString:self.dataHandler.password])
    {
        
        [self.view makeToast:@"当前密码和原密码不一致"];
        return;
    }

    else if (![self.textMM2.text isEqualToString:self.textMM3.text])
    {
    
        [self.view makeToast:@"输入新密码前后不一致"];
        return;
    
    }
 
    
    //MD5加密操作
    NSString *newPassword = md5(self.textMM2.text);
    
    
    
    //输入密码进行网络修改
    NSString *urlStr = [NSString stringWithFormat:@"%@update_wholesaler",SERVER_ADDRESS2];
    NSDictionary *params = @{@"wTel" : self.dataHandler.phone , @"uToken" : self.dataHandler.uToken , @"passwd" : newPassword};
    
    
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    
    [self.manager POST:urlStr parameters:params success:^(NSURLSessionDataTask *task, id responseObject) {
        
//        NSLog(@"ashjdhas  %@",responseObject);
       
        if ([responseObject[@"error_flag"] intValue] == 0) {
            
            
            
            //修改以前保存的密码，然后进行存储
            
            self.dataHandler.password = self.textMM2.text;
            [self.dataHandler saveLocalData:@"password" value:self.dataHandler.password];
            
            
            [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
            
            UIAlertView *alter= [[UIAlertView alloc]initWithTitle:@"提示" message:@"修改密码成功" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
            [alter show];
            
            [self.navigationController popViewControllerAnimated:YES];
            
            
            
        }
        else
        {
            
            
              [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
            NSString *str = [NSString stringWithFormat:@"%@",responseObject[@"result_msg"]];
            UIAlertView *alter = [[UIAlertView alloc]initWithTitle:@"提示" message:str delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
            [alter show];
            
            
            
        }

        
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
        [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
        UIAlertView *alter = [[UIAlertView alloc]initWithTitle:@"提示" message:@"请检查网络" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
        [alter show];
        
        
    }];
    
    
    
    
    
    
    
}



-(void)textFieldDidBeginEditing:(UITextField *)textField
{
    
    
    if (ViewHeight == 480.0) {
   
    
    if (textField == self.textMM3) {
        
    

    [UIView animateWithDuration:0.25 animations:^{
        if (self.view.y == 0) {
            self.view.y = -100;
        }
    }completion:^(BOOL finished){
        //生成pickView
        
        
        
    }];

    }
    }
}


#pragma mark 收回键盘操作
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{

    [textField resignFirstResponder];
    
    
    [UIView animateWithDuration:0.25 animations:^{
        if (self.view.y == -100) {
            self.view.y = 0;
        }
    }completion:^(BOOL finished){
        //生成pickView
        
        
        
    }];
    
    
    return YES;



}



@end
